package com.alumni_circle.controller.me;

import com.alumni_circle.core.config.SysLog;
import com.alumni_circle.core.entity.BaseId;
import com.alumni_circle.core.entity.PageBase;
import com.alumni_circle.core.entity.ResponseListModel;
import com.alumni_circle.core.entity.ResponseModel;
import com.alumni_circle.core.utils.ModelUtils;
import com.alumni_circle.core.web.BaseController;
import com.alumni_circle.entity.me.Authentication;
import com.alumni_circle.entity.me.User;
import com.alumni_circle.entity.me.VO.AuthenticationInsertVO;
import com.alumni_circle.entity.me.VO.AuthenticationSearchVO;
import com.alumni_circle.entity.me.VO.AuthenticationUpdateVO;
import com.alumni_circle.entity.me.VO.AuthenticationViewVO;
import com.alumni_circle.service.me.impl.AuthenticationServiceImpl;
import com.alumni_circle.service.me.impl.UserServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.awt.*;
import java.util.List;

@Controller
@RequestMapping("/authentication")
public class AuthenticationController extends BaseController<Authentication> {

    @Resource
    AuthenticationServiceImpl authenticationService;

    @Resource
    UserServiceImpl userService;

    @ResponseBody
    @RequestMapping(value = "/addAuthentication", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "增加认证信息", notes = "增加认证信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="增",accessInfo="增加认证信息",url="/authentication/addAuthentication")
    public ResponseModel addAuthentication(@ApiParam(required = true, name = "baseId")
                                    @RequestBody AuthenticationInsertVO authenticationInsertVO) {
        authenticationService.save(ModelUtils.transform(authenticationInsertVO,Authentication.class));
        return new ResponseModel<>();
    }

    @ResponseBody
    @RequestMapping(value = "/searchAuthentication", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "搜索认证信息", notes = "搜索认证信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="搜",accessInfo="搜索认证信息",url="/authentication/searchAuthentication")
    public ResponseListModel searchAuthentication(@ApiParam(required = true, name = "baseId")
                                           @RequestBody AuthenticationSearchVO authenticationSearchVO) {
        PageHelper.startPage(authenticationSearchVO.getPageNum(),authenticationSearchVO.getPageSize());
        List<AuthenticationViewVO> list = authenticationService.search(authenticationSearchVO.getUsername(),authenticationSearchVO.getClub());
        PageInfo<AuthenticationViewVO> pageInfo = new PageInfo<>(list);
        return new ResponseListModel<>(list,String.valueOf(pageInfo.getTotal()));
    }

    @ResponseBody
    @RequestMapping(value = "/getAllAuthentication", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "增加认证信息", notes = "增加认证信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="增",accessInfo="增加认证信息",url="/authentication/getAllAuthentication")
    public ResponseListModel getAllAuthentication(@ApiParam(required = true, name = "baseId")
                                           @RequestBody PageBase pageBase) {
        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
        List<AuthenticationViewVO> list = authenticationService.getAll();
        PageInfo<AuthenticationViewVO> pageInfo = new PageInfo<>(list);
        return new ResponseListModel<>(list,String.valueOf(pageInfo.getTotal()));
    }

    @ResponseBody
    @RequestMapping(value = "/updateAuthentication", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "更新认证信息", notes = "更新认证信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="改",accessInfo="更新认证信息",url="/authentication/updateAuthentication")
    public ResponseModel updateAuthentication(@ApiParam(required = true, name = "authentication")
                                    @RequestBody AuthenticationUpdateVO authenticationUpdateVO) {
        boolean bool = authenticationService.updateById(ModelUtils.transform(authenticationUpdateVO,Authentication.class));
        if(bool){
            User user = new User();
            user.setId(authenticationUpdateVO.getUserId());
            user.setClub(authenticationUpdateVO.getClub());
            userService.updateById(user);
        }
        return new ResponseModel<>();
    }
}
